package controller

import (
	"context"
	v1 "itam/api/v1"
	"itam/internal/model"
	"itam/internal/service"
)

type cVm struct{}

var Vm = cVm{}

func (c *cVm) Create(ctx context.Context, req *v1.VmCreateReq) (res *v1.VmCreateRes, err error) {
	out, err := service.Vm().Create(ctx, model.VmCreateInput{
		Name:           req.Name,
		OsType:         req.OsType,
		Owner:          req.Owner,
		Vcpu:           req.Vcpu,
		Memory:         req.Memory,
		Disk:           req.Disk,
		IpAddress:      req.IpAddress,
		PmId:           req.PmId,
		EnvId:          req.EnvId,
		MachineUsage:   req.MachineUsage,
		RecomandStatus: req.RecomandStatus,
		ExpiredDate:    req.ExpiredDate,
	})
	if err != nil {
		return nil, err
	}
	return &v1.VmCreateRes{
		Id: out.Id,
	}, nil
}

func (c *cVm) Delete(ctx context.Context, req *v1.VmDeleteReq) (res *v1.VmDeleteRes, err error) {
	err = service.Vm().Delete(ctx, req.Id)
	return
}

func (c *cVm) Update(ctx context.Context, req *v1.VmUpdateReq) (res *v1.VmUpdateRes, err error) {
	err = service.Vm().Update(ctx, model.VmUpdateInput{
		Id:             req.Id,
		OsType:         req.OsType,
		Owner:          req.Owner,
		Vcpu:           req.Vcpu,
		Memory:         req.Memory,
		Disk:           req.Disk,
		IpAddress:      req.IpAddress,
		PmId:           req.PmId,
		EnvId:          req.EnvId,
		MachineUsage:   req.MachineUsage,
		RecomandStatus: req.RecomandStatus,
		ExpiredDate:    req.ExpiredDate,
	})
	if err != nil {
		return nil, err
	}
	return
}

func (c *cVm) GetList(ctx context.Context, req *v1.VmGetListReq) (res *v1.VmGetListRes, err error) {
	out, err := service.Vm().GetList(ctx, model.VmGetListInput{
		Name:           req.Name,
		OsType:         req.OsType,
		Owner:          req.Owner,
		IpAddress:      req.IpAddress,
		PmId:           req.PmId,
		EnvId:          req.EnvId,
		RecomandStatus: req.RecomandStatus,
		Page:           req.Page,
		Size:           req.Size,
	})
	if err != nil {
		return nil, err
	}
	return &v1.VmGetListRes{
		List:  out.List,
		Page:  out.Page,
		Size:  out.Size,
		Total: out.Total,
	}, nil
}
